home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / FEGrid.cc < prev    next >
C/C++ Source or Header  |  1996-03-03  |  2KB  |  141 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include <iostream.h>
  32.  
  33. #include "FEGrid.h"
  34. #include "lo-error.h"
  35.  
  36. // error handling
  37.  
  38. void
  39. FEGrid::error (const char* msg) const
  40. {
  41.   (*current_liboctave_error_handler) ("fatal FEGrid error: %s", msg);
  42. }
  43.  
  44. void
  45. FEGrid::nel_error (void) const
  46. {
  47.   error ("number of elements less than 1");
  48. }
  49.  
  50. // Constructors
  51.  
  52. FEGrid::FEGrid (int nel, double width)
  53. {
  54.   if (nel < 1)
  55.     {
  56.       nel_error ();
  57.       return;
  58.     }
  59.  
  60.   elem.resize (nel+1);
  61.  
  62.   for (int i = 0; i <= nel; i++)
  63.     elem.elem (i) = i * width;
  64. }
  65.  
  66. FEGrid::FEGrid (int nel, double left, double right)
  67. {
  68.   if (nel < 1)
  69.     {
  70.       nel_error ();
  71.       return;
  72.     }
  73.  
  74.   elem.resize (nel+1);
  75.  
  76.   double width = (right - left) / (double) nel;
  77.  
  78.   for (int i = 0; i <= nel; i++)
  79.     elem.elem (i) = i * width + left;
  80.  
  81.   check_grid ();
  82. }
  83.  
  84. int
  85. FEGrid::element (double x) const
  86. {
  87.   if (! in_bounds (x))
  88.     {
  89.       error ("value not within grid boundaries");
  90.       return -1;
  91.     }
  92.  
  93.   int nel = elem.capacity () - 1;
  94.   for (int i = 1; i <= nel; i++)
  95.     {
  96.       if (x >= elem.elem (i-1) && x <= elem.elem (i))
  97.     return i;
  98.     }
  99.   return -1;
  100.        
  101. }
  102.  
  103. void
  104. FEGrid::check_grid (void) const
  105. {
  106.   int nel = elem.capacity () - 1;
  107.   if (nel < 1)
  108.     {
  109.       nel_error ();
  110.       return;
  111.     }
  112.  
  113.   for (int i = 1; i <= nel; i++)
  114.     {
  115.       if (elem.elem (i-1) > elem.elem (i))
  116.     {
  117.       error ("element boundaries not in ascending order");
  118.       return;
  119.     }
  120.  
  121.       if (elem.elem (i-1) == elem.elem (i))
  122.     {
  123.       error ("zero width element");
  124.       return;
  125.     }
  126.     }
  127. }
  128.  
  129. ostream&
  130. operator << (ostream& s, const FEGrid& g)
  131. {
  132.   s << g.element_boundaries ();
  133.   return s;
  134. }
  135.  
  136. /*
  137. ;;; Local Variables: ***
  138. ;;; mode: C++ ***
  139. ;;; End: ***
  140. */
  141.